<template>
  <div class="imgDialog width_100 height_100" v-if="showThisDialog">
    <div class="box">
      <div class="head">
        <span class="title">图形验证码</span>
        <img
          class="closebtn"
          @click="toggleShow(false)"
          src="@/assets/dialogBox/images/close.png"
          alt=""
        />
        <!-- <span class="closebtn el-icon-close" @click="closeEvent"></span> -->
      </div>
      <div class="content">
        <input type="text" placeholder="请输入图形验证码" v-model="imgCode" :class="{'error': errorMessage}" @input="resetError">
        <img :src="src" class="cp" alt="暂无" @click="getImgCode">
      </div>
      <div class="errorMessage">{{ errorMessage }}</div>
      <div class="foot flex-center">
        <div class="btn-blue" @click="confirm">确定</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      src: '',
      phone: '',
      errorMessage: ''
    }
  },
  computed: {
    showThisDialog () {
      return this.$store.state.common.showImgDialog
    },
    // 图形验证码
    imgCode: {
      get () {
        return this.$store.state.common.imgCode
      },
      set (val) {
        this.$store.commit('common/set_imgCode', val)
      }
    }
  },
  methods: {
    // 显示隐藏
    toggleShow (flag) {
      this.$store.commit('common/set_showImgDialog', flag)
    },
    // 确认
    confirm () {
      if (this.imgCode.trim().length) {
        this.$store.dispatch('common/confirm').then(res => {
          if (res.data.code === 502) {
            this.errorMessage = res.data.message
          } else {
            this.toggleShow(false)
          }
        })
      } else {
        this.errorMessage = '请输入图形验证码'
      }
    },
    // 获取图形验证码图片
    getImgCode () {
      this.$store.dispatch('common/getImgCode', this.phone).then(res => {
        if (res.data.code === 200) {
          this.src = 'data:image/jpeg;base64,' + res.data.data
        } else {
          this.errorMessage = res.data.message
        }
      })
    },
    // 获取手机号
    getPhone (phone) {
      this.phone = phone
      this.getImgCode()
    },
    // 重置错误信息
    resetError () {
      if (this.errorMessage) {
        this.errorMessage = ''
      }
    }
  },
  beforeDestroy () {
    this.$store.commit('common/set_showImgDialog', false)
  }
}
</script>

<style lang="scss" scoped>
.imgDialog {
  position: fixed;
  z-index: 1999;
  top: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.49);
  .box {
    width: 560px;
    height: 300px;
    margin: 0 auto;
    background-color: #fff;
    border-radius: 5px;
    margin-top: 260px;
    position: relative;
    .head {
      overflow: hidden;
      padding: 20px;
      .title {
        float: left;
        height: 25px;
        font-size: 18px;
        color: #0D172C;
        line-height: 25px;
        letter-spacing: 0px;
      }
      .closebtn {
        float: right;
        width: 18px;
        font-weight: 200;
        color: #bbc4d7;
        // padding-right: 20px;
        cursor: pointer;
      }
    }
    .content {
      position: absolute;
      width: 100%;
      top: 84px;
      padding-left: 60px;
      input{
        width: 308px;
        height: 44px;
        border: 1px solid #d8dee9;
        border-radius: 2px;
        padding-left: 12px;
      }
      .error {
        border-color: #FF2929;
      }
      img{
        margin-left: 12px;
        width: 120px;
        height: 44px;
        vertical-align: middle;
      }
    }
    .errorMessage {
      position: absolute;
      top: 135px;
      font-size: 12px;
      color: #FF2929;
      left: 60px;
    }
    .foot {
      width: 100%;
      height: 42px;
      position: absolute;
      bottom: 30px;
    }
  }
}
</style>
